package data_report

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DataReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 首充二充三充报表
func NewDataReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DataReportLogic {
	return &DataReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DataReportLogic) DataReport(req *types.ChargeReportRequest) (resp types.ChargeReportResponse, err error) {

	resp = types.ChargeReportResponse{
		Data:     make([]types.ChargeReportData, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	reqq := &rpc.SearchArUserRechargeCountAnalysisReq{
		Page:         req.Page,
		Limit:        req.PageSize,
		CreatedAt:    req.StartTime,
		UserId:       req.UserId,
		OrderNo:      req.OrderNo,
		FinishAt:     req.EndTime,
		Source:       req.ChannelId,
		RechargeType: req.Type,
	}

	analysis, err := l.svcCtx.Analysis.SearchArUserRechargeCountAnalysis(l.ctx, reqq)
	if err != nil {
		return resp, err
	}

	for _, v := range analysis.ArUserRechargeCountAnalysis {
		t := types.ChargeReportData{
			Date:         strconv.FormatInt(v.CreatedAt, 10),
			UserId:       v.UserId,
			ParentId1:    v.ParentId,
			ParentId2:    v.UpupParentId,
			ParentId3:    v.UpupupParentId,
			Account:      v.UserAccount,
			OrderNo:      v.OrderNo,
			Amount:       v.RechargeAmount,
			CompleteTime: v.FinishAt,
		}
		resp.Data = append(resp.Data, t)
	}
	resp.Total = analysis.Total
	return
}
